package Library.items;

import Library.dbcommands.DBConnector;
import Library.dbcommands.IDBCommands;
/**
 * 
 * @author Sansarova
 *
 */
public class User implements IUser{
	private String username;
	private String password;
	private String firstName;
	private String lastName;
	private int age;
	private String gender;
	private String phone;
	private String address;
	private String egn;
	private static IDBCommands db=DBConnector.connector();
	
	public User(){
		
	}

	public User(String username, String pass, String fname, String lname,
			int age, String gender, String phone, String address, String egn) {
		this(username,pass);
		if(isValidParam(fname)){
			this.firstName=fname;
		}
		if(isValidParam(lname)){
			this.lastName=lname;
		}
		if(isValidParam(phone)){
			this.phone=phone;
		}
		if(isValidParam(gender)){
			this.gender=gender;
		}
		if(isValidParam(address)){
			this.address=address;
		}
		if(age>0){
			this.age=age;
		}
		if(isValidParam(egn)){
			this.egn=egn;
		}

	}

	User(String usern, String pass){
		if(isValidParam(usern)){
			username=usern;
		}
		if(isValidParam(pass)){
			password=pass;
		}
	}
	
	public boolean canInsertUser(){
		return db.addUser(this);
		
	}
	
	public static User ExistingUser(String name, String pass){
		User user=new User(name,pass);
		for(User u :db.getAllUser()){
			if(u.equals(user)){
				return u;
			}
		}
		return null;
	}
	
	public static User ExistingUser(String username, String password, String fname,String lname,int age,String gender,String phone,String address,String egn){
		User user=new User(username,password);
		for(User u :db.getAllUser()){
			if(u.equals(user)){
				return u;
			}
		}
		return null;
	}

	public String getEgn() {
		return egn;
	}

	private boolean isValidParam(String pass) {
		return pass!=null && !pass.equals("");
	}

	public String getUsername() {
		return username;
	}

	public String getPassword() {
		return password;
	}

	public String getFirstName() {
		return firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public int getAge() {
		return age;
	}

	public String getGender() {
		return gender;
	}

	public String getPhone() {
		return phone;
	}

	public String getAddress() {
		return address;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((password == null) ? 0 : password.hashCode());
		result = prime * result
				+ ((username == null) ? 0 : username.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		if (username == null) {
			if (other.username != null)
				return false;
		} else if (!username.equals(other.username))
			return false;
		return true;
	}
	
	public String print(){
		return "first name: "+firstName+" last name: "+lastName+" username: "+username+
				" age: "+age+" gender: "+gender+ " phone: "+phone+" address: "+address+" password:  "+password+
				" egn: "+egn;
	}

}
